package com.huika.dealontop.control.base.extend;

import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ListView;

import com.huika.dealontop.control.base.extend.impl.ILoadableListProvider;
import com.huika.dealontop.utils.DebugTool;
import com.huika.dealontop.utils.UIUtils;

/**
 * Author: Victor Wu
 * Email: victor-wu*foxmail.com
 * Time: 2015/11/13 15:54
 * Description: TODO
 */
public abstract class BaseLoadableListFragment<T, E> extends BaseLoadableFragment<T> implements ILoadableListProvider<T, E>, SwipeRefreshLayout.OnRefreshListener {
    private SwipeRefreshLayout mRefreshLayout;

    private ListView mListView;

    protected BaseLoadMoreAdapter<T, E> mListAdapter;

//    protected int currPageNumber = 1;

    protected int mPageSize = 10;

    @Override
    public View onCreateSuccessView(T resultData) {
//        currPageNumber = 1;
        if (needToRefresh()) {
            return getSwipeRefreshLayout(resultData);
        }
        return getListView(resultData);
    }


    public View getSwipeRefreshLayout(T resultData) {
        if (mRefreshLayout == null) {
            mRefreshLayout = new SwipeRefreshLayout(UIUtils.getContext());
            mRefreshLayout.addView(getListView(resultData));
            mRefreshLayout.setOnRefreshListener(this);
        } else {
            //重新设置数据  这个方法中自动刷新数据
            mListAdapter.setData(getListData(resultData));
        }

        mRefreshLayout.setRefreshing(false);

        return mRefreshLayout;
    }

    public ListView getListView(T resultData) {
        if (mListView == null) {
            // adapter ---> list
            mListView = new ListView(UIUtils.getContext());
            //兼容 StickyNavLayout //TODO
//            mListView.setId(R.id.id_stickynavlayout_innerscrollview);
            mListAdapter = new BaseLoadMoreAdapter<>(mListView, getListData(resultData), BaseLoadableListFragment.this);
            //必须在setAdapter 之前设置
            //mListAdapter.setListProvider(BaseLoadableListFragment.this);
            mListView.setAdapter(mListAdapter);
            //刷新数据
            mListAdapter.notifyDataSetChanged();
        } else {
            //重新设置数据  这个方法中自动刷新数据
            mListAdapter.setData(getListData(resultData));
        }
        return mListView;
    }

    @Override
    public void onPagerStateChanged(int oldState, int newState) {
        if (StatusPage.STATE_EMPTY == newState || StatusPage.STATE_ERROR == newState) {
            //加载数据失败 ， 或者数据为空
            DebugTool.print("加载数据失败 ...");
            if (mRefreshLayout != null) {
                mRefreshLayout.setRefreshing(false);
            }
        }
    }

    @Override
    public void onRefresh() {
        mRefreshLayout.setRefreshing(true);
        //刷新前 可能已经还在执行加载更多的任务
        mListAdapter.cancelLoadMore();
        //刷新数据
        startLoadData();
    }


    protected boolean needToRefresh() {
        return true;
    }

    @Override
    public boolean needToLoadMore() {
        return true;
    }

    @Override
    public int getPageSize() {
        return mPageSize;
    }

    public void setPageSize(int pageSize){
        this.mPageSize = pageSize;
    }

    public int getCurrPageNumber() {
        return mListAdapter.getCount() + (mPageSize - 1)  / mPageSize;
    }

    public SwipeRefreshLayout getRefreshLayout() {
        return mRefreshLayout;
    }

    public void setRefreshLayout(SwipeRefreshLayout mRefreshLayout) {
        this.mRefreshLayout = mRefreshLayout;
    }

    public ListView getListView() {
        return mListView;
    }

    public void setListView(ListView mListView) {
        this.mListView = mListView;
    }

    public BaseLoadMoreAdapter getListAdapter() {
        return mListAdapter;
    }

    public void setListAdapter(BaseLoadMoreAdapter<T, E> mListAdapter) {
        this.mListAdapter = mListAdapter;
    }

}
